package com.shu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shu.pojo.Article;
import com.shu.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shu
 * @date 2021/3/23
 */

/**
 * 文章数据控制器
 */
@Controller
@RequestMapping("/data")
public class DataController {

    @Autowired
    private   ArticleService articleService;

    @RequestMapping("/search/{tid}/{pages}")
    public String Search(Integer wid, Model model,@PathVariable("tid") Integer tid,@PathVariable("pages") Integer pages){
        QueryWrapper<Article> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("wid",wid);
        Article article = articleService.getOne(wrapper1);
        //页码封装
        List<Integer> page_all=new ArrayList<>();
        QueryWrapper<Article> wrapper = new QueryWrapper<Article>();
        //分页数据
        Page<Article> articlePage = new Page<>(pages,10);
        wrapper.eq("tid",tid);
        Page<Article> list = articleService.page(articlePage, wrapper);
        for (int i = 1; i <list.getPages() ; i++) {
            page_all.add(i);
        }
        model.addAttribute("list",list);
        model.addAttribute("page",page_all);
        model.addAttribute("article",article);
        if(tid==1){
        return "server";
        }
        else if(tid==2){
        return "business";
        }else if (tid==3){
        return "projects";
        }else if(tid==4){
        return "pricing";
        }
        return "contact";
    }
}
